/**
 * @encoding UTF-8
 */
package orgx.wxyz.util;

/**
 * <p>
 * RGB颜色类
 * </p>
 * @author hyhjwzx@126.com
 * @date 2013-4-16 下午12:07:02
 */
public final class RGBColor {
	/**
	 * 红色值0-255
	 */
	public final int red;
	
	/**
	 * 绿色值0-255
	 */
	public final int green;
	
	/**
	 * 蓝色值0-255
	 */
	public final int blue;
	
	/**
	 * <p>
	 * 构造一个颜色对象，所有颜色值在构造器里初始化
	 * </p>
	 * @param red 红色值0-255
	 * @param green 绿色值0-255
	 * @param blue 蓝色值0-255
	 */
	public RGBColor(int red, int green, int blue) {
		// 验证颜色值否是越界
		if (red > 255 && red < 0 
				|| green > 255 && green < 0
				|| blue > 255 && blue < 0) {
			throw new IllegalArgumentException("颜色值错误！");
		}
		
		this.red = red;
		this.green = green;
		this.blue = blue;
	}
	
	/**
	 * <p>
	 * 将分开的rgb值合并成一个颜色值
	 * </p>
	 * 
	 * @param rgb 颜色对象
	 * @return 合并后的RGB值
	 */
	public int concatRGB() {
		int rgb = 0;
		
		rgb = this.red << 16;
		rgb += this.green << 8;
		rgb += this.blue;
		
		return rgb;
	}
	
	/**
	 * <p>
	 * 将一个颜色RGB拆分成单个的rgb值
	 * </p>
	 * 
	 * @param rgb 颜色值
	 * @return 拆分后的颜色值
	 */
	public static RGBColor splitRGB(int rgb) {
		int red = (rgb & 0x00FF0000) >>> 16;
		int green = (rgb & 0x0000FF00) >>> 8;
		int blue = rgb & 0x000000FF;
		
		return new RGBColor(red, green, blue);
	}
	
	/**
	 * <p>
	 * 重写toString()方法，输出一个十六进制颜色值的对象json字符串
	 * </p>
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "{red=0x" + Integer.toHexString(red).toUpperCase() + 
			", green=0x" + Integer.toHexString(green).toUpperCase() + 
			", blue=0x" + Integer.toHexString(blue).toUpperCase() + "}";
	}
}
